package com.maistars.spider.repository.dao.mybatis;

import com.maistars.spider.common.exception.TopicSpiderException;
import com.maistars.spider.repository.dao.CrawlerTaskDao;
import com.maistars.spider.repository.mapper.CrawlerFieldRuleMapper;
import com.maistars.spider.repository.mapper.CrawlerPageRuleMapper;
import com.maistars.spider.repository.mapper.CrawlerSiteRuleMapper;
import com.maistars.spider.repository.mapper.CrawlerTaskMapper;
import com.maistars.spider.repository.model.CrawlerFieldRulePO;
import com.maistars.spider.repository.model.CrawlerPageRulePO;
import com.maistars.spider.repository.model.CrawlerSiteRulePO;
import com.maistars.spider.repository.model.CrawlerTaskPO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author dhr
 * @date 2021/10/1 下午11:59
 */
@Service
public class MybatisCrawlerTaskDao implements CrawlerTaskDao {

    @Resource
    private CrawlerTaskMapper crawlerTaskMapper;

    @Resource
    private CrawlerSiteRuleMapper crawlerSiteRuleMapper;

    @Resource
    private CrawlerPageRuleMapper crawlerPageRuleMapper;

    @Resource
    private CrawlerFieldRuleMapper crawlerFieldRuleMapper;

    @Override
    public void createCrawlerTask(CrawlerTaskPO crawlerTaskPO) throws TopicSpiderException {
        try {
            crawlerTaskMapper.insert(crawlerTaskPO);
        } catch (Throwable e) {
            throw new TopicSpiderException("createCrawlerTask occured error", e);
        }
    }

    @Override
    public void createCrawlerSiteRule(CrawlerSiteRulePO crawlerSiteRulePO) throws TopicSpiderException {
        try {
            crawlerSiteRuleMapper.insert(crawlerSiteRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("addCrawlerPageRule occured error", e);
        }
    }

    @Override
    public void createCrawlerPageRule(CrawlerPageRulePO crawlerPageRulePO) throws TopicSpiderException {
        try {
            crawlerPageRuleMapper.insert(crawlerPageRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("addCrawlerPageRule occured error", e);
        }
    }

    @Override
    public void createCrawlerFieldRule(CrawlerFieldRulePO crawlerFieldRulePO) throws TopicSpiderException {
        try {
            crawlerFieldRuleMapper.insert(crawlerFieldRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("addCrawlerFieldRule occured error", e);
        }
    }

    @Override
    public void updateCrawlerTask(CrawlerTaskPO crawlerTaskPO) throws TopicSpiderException {
        try {
            crawlerTaskMapper.updateByPrimaryKeySelective(crawlerTaskPO);
        } catch (Throwable e) {
            throw new TopicSpiderException("updateCrawlerTask occured error", e);
        }
    }

    @Override
    public void updateCrawlerSiteRule(CrawlerSiteRulePO crawlerSiteRulePO) throws TopicSpiderException {
        try {
            crawlerSiteRuleMapper.updateByPrimaryKeySelective(crawlerSiteRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("updateCrawlerTask occured error", e);
        }
    }

    @Override
    public void updateCrawlerPageRule(CrawlerPageRulePO crawlerPageRulePO) throws TopicSpiderException {
        try {
            crawlerPageRuleMapper.updateByPrimaryKeySelective(crawlerPageRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("updateCrawlerTask occured error", e);
        }
    }

    @Override
    public void updateCrawlerFieldRule(CrawlerFieldRulePO crawlerFieldRulePO) throws TopicSpiderException {
        try {
            crawlerFieldRuleMapper.updateByPrimaryKeySelective(crawlerFieldRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("updateCrawlerTask occured error", e);
        }
    }

    @Override
    public CrawlerTaskPO getCrawlerTask(Integer taskId) throws TopicSpiderException {
        try {
            return crawlerTaskMapper.selectByPrimaryKey(taskId);
        } catch (Throwable e) {
            throw new TopicSpiderException("getCrawlerTask occured error", e);
        }
    }

    @Override
    public CrawlerPageRulePO getCrawlerPageRule(Integer pageRuleId) throws TopicSpiderException {
        try {
            return crawlerPageRuleMapper.selectByPrimaryKey(pageRuleId);
        } catch (Throwable e) {
            throw new TopicSpiderException("getCrawlerTask occured error", e);
        }
    }

    @Override
    public CrawlerSiteRulePO getCrawlerSiteRule(Integer siteRuleId) throws TopicSpiderException {
        try {
            return crawlerSiteRuleMapper.selectByPrimaryKey(siteRuleId);
        } catch (Throwable e) {
            throw new TopicSpiderException("getCrawlerPageRules occured error", e);
        }
    }

    @Override
    public List<CrawlerTaskPO> listCrawlerTasks() throws TopicSpiderException {
        try {
            return crawlerTaskMapper.selectAll();
        } catch (Throwable e) {
            throw new TopicSpiderException("listCrawlerTasks occured error", e);
        }
    }

    @Override
    public List<CrawlerSiteRulePO> listCrawlerSiteRules(Integer taskId) throws TopicSpiderException {
        try {
            CrawlerSiteRulePO crawlerSiteRulePO = new CrawlerSiteRulePO();
            crawlerSiteRulePO.setTaskId(taskId);
            return crawlerSiteRuleMapper.queryByCondition(crawlerSiteRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("listCrawlerSiteRules occured error", e);
        }
    }

    @Override
    public CrawlerFieldRulePO getCrawlerFieldRule(Integer fieldRuleId) throws TopicSpiderException {
        try {
            return crawlerFieldRuleMapper.selectByPrimaryKey(fieldRuleId);
        } catch (Throwable e) {
            throw new TopicSpiderException("getCrawlerPageRules occured error", e);
        }
    }

    @Override
    public CrawlerFieldRulePO getCrawlerFieldRuleByName(String fieldId, Integer pageRuleId) throws TopicSpiderException {
        try {
            CrawlerFieldRulePO crawlerFieldRulePO = new CrawlerFieldRulePO();
            crawlerFieldRulePO.setFieldId(fieldId);
            crawlerFieldRulePO.setPageRuleId(pageRuleId);
            crawlerFieldRulePO.setStatus(0);
            List<CrawlerFieldRulePO> crawlerFieldRulePOS = crawlerFieldRuleMapper.queryByCondition(crawlerFieldRulePO);
            if (crawlerFieldRulePOS != null && !crawlerFieldRulePOS.isEmpty()) {
                return crawlerFieldRulePOS.get(0);
            }
            return null;
        } catch (Throwable e) {
            throw new TopicSpiderException("getCrawlerFieldRuleByName occured error", e);
        }
    }

    @Override
    public List<CrawlerPageRulePO> listCrawlerPageRules(Integer taskId) throws TopicSpiderException {
        try {
            CrawlerPageRulePO crawlerPageRulePO = new CrawlerPageRulePO();
            crawlerPageRulePO.setTaskId(taskId);
            crawlerPageRulePO.setStatus(0);
            return crawlerPageRuleMapper.queryByCondition(crawlerPageRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("listCrawlerPageRules occured error", e);
        }
    }

    @Override
    public List<CrawlerFieldRulePO> listCrawlerFieldRules(Integer pageRuleId) throws TopicSpiderException {
        try {
            CrawlerFieldRulePO crawlerFieldRulePO = new CrawlerFieldRulePO();
            crawlerFieldRulePO.setPageRuleId(pageRuleId);
            crawlerFieldRulePO.setStatus(0);
            return crawlerFieldRuleMapper.queryByCondition(crawlerFieldRulePO);
        } catch (Throwable e) {
            throw new TopicSpiderException("listCrawlerFieldRules occured error", e);
        }
    }
}
